package cn.newtest.common.dbutils.dialect;

/**
 * <p>Title: SQLServerDialect.java</p>
 * <p>Date：Oct 28, 2009
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) risesoft Corporation</p>
 * <p>Company: risesoft</p>
 * @author liubx (lbx_508@163.com)
 * @version 1.0
 */

public class SQLServerDialect implements DBDialect{

	public boolean supportsLimit() {
		return true;
	}
	
	public String getCountString(String sql) {
		StringBuffer countSelect = new StringBuffer(100);
		countSelect.append("select count(*) from (");
		countSelect.append(sql);
		countSelect.append(" ) totalTable ");
		return countSelect.toString();
	}

	public String getLimitString(String sql) {
		// TODO Auto-generated method stub
		return null;
	}

	public String getLimitString(String sql, int begin, int end, String orderByKey) {
		StringBuffer pagingSelect = new StringBuffer(100);
		int pageSize = end - begin;
		
		pagingSelect.append(" select TOP " + pageSize + " * from ( " + sql + " ) a");
		pagingSelect.append(" WHERE ");
		pagingSelect.append(" ( ").append(orderByKey).append(">=");
		pagingSelect.append(" (SELECT MAX(" + orderByKey + ") FROM ");
		pagingSelect.append(" (SELECT TOP ").append(begin+1).append(" ").append(orderByKey).append(" FROM (").append(sql).append(")as b");
		pagingSelect.append(" ORDER BY " + orderByKey + ") AS T))ORDER BY a."
				+ orderByKey);
		return pagingSelect.toString();
	}

	 

}
